#include	<string.h>
#include	<unistd.h>
#include	<stdlib.h>

int		main(int ac, char **av)
{
  int		pipefd[2];
  int		error;
  int		pid;
  int		len;
  char		buff[512];

  error = pipe(pipefd);
  if (error == -1)
    {
      write(2, "Pipe failed\n", strlen("Pipe failed\n"));
      exit(EXIT_FAILURE);
    }
  if ((pid = fork()) == -1)
    {
      write(2, "Fork failed\n", strlen("Fork failed\n"));
      exit(EXIT_FAILURE);
    }
  if (pid == 0)
    {
      close(pipefd[0]);
      error = dup2(fd, 1);
      if (error == -1)
	{
	  write(2, "Fork failed\n", strlen("Fork failed\n"));
	  exit(EXIT_FAILURE);
	}
      write(pipefd[1], "Salut\n", 6);
    }
  else
    {
      close(pipefd[1]);
      fd = open("toto", O_RDONLY, O_CREAT);
      len = read(pipefd[0], buff, 511);
      if (len == -1)
	{
	  write(2, "Read failed\n", strlen("Read failed\n"));
	  exit(EXIT_FAILURE);
	}
      buff[len] = '\0';
      write(1, buff, len);
    }
  return (EXIT_SUCCESS);
}
